package StudentAchievementManagementSystem.ui;

import StudentAchievementManagementSystem.util.ScreenUtils;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

public class ManagerInterface {
    JFrame jf = new JFrame("学生成绩管理系统：");

    final int WIDTH = 1000;
    final int HEIGHT = 600;

    //组装视图
    public void init() throws Exception {
        //给窗口设置属性
        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        jf.setResizable(false);

        //设置菜单栏
        JMenuBar jmb = new JMenuBar();
        JMenu jMenu = new JMenu("设置");
        JMenuItem m1 = new JMenuItem("切换账号");
        JMenuItem m2 = new JMenuItem("退出程序");

        m1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new LoginScreen().init();
                    jf.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        m2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        jMenu.add(m1);
        jMenu.add(m2);
        jmb.add(jMenu);

        jf.setJMenuBar(jmb);

        //设置分割面板
        JSplitPane sp = new JSplitPane();

        //支持连续布局
        sp.setContinuousLayout(true);
        sp.setDividerLocation(150);
        sp.setDividerSize(7);

        //设置左侧内容
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("功能界面");
        DefaultMutableTreeNode systemManage = new DefaultMutableTreeNode("系统设置");
        DefaultMutableTreeNode stuGra = new DefaultMutableTreeNode("成绩查询");
        DefaultMutableTreeNode gradeIns = new DefaultMutableTreeNode("成绩录入");
        DefaultMutableTreeNode inforChange = new DefaultMutableTreeNode("信息更新");
        DefaultMutableTreeNode inforStat = new DefaultMutableTreeNode("信息统计");
        DefaultMutableTreeNode inforPrint = new DefaultMutableTreeNode("信息打印");

        root.add(systemManage);
        root.add(stuGra);
        root.add(gradeIns);
        root.add(inforChange);
        root.add(inforStat);
        root.add(inforPrint);

        JTree tree = new JTree(root);
        MyRenderer myRenderer = new MyRenderer();
        Color color =new Color(201,220,198);
        myRenderer.setBackgroundNonSelectionColor(color);
        myRenderer.setBackgroundSelectionColor(new Color(140,140,140));
        tree.setCellRenderer(myRenderer);

        tree.setBackground(color);
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            //当条目选中变化后，这个方法会执行
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                //得到当前选中的结点对象
                Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();

                if (systemManage.equals(lastPathComponent)){
                    sp.setRightComponent(new JLabel("这里进行系统管理..."));
                    sp.setDividerLocation(150);
                }else if (stuGra.equals(lastPathComponent)){
                    try {
                        sp.setRightComponent(new StuGraComponent(jf));
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (ClassNotFoundException ex) {
                        ex.printStackTrace();
                    }
                    sp.setDividerLocation(150);
                }else if (gradeIns.equals(lastPathComponent)){
                    try {
                        sp.setRightComponent(new GradeInsComponent(jf));
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    } catch (ClassNotFoundException ex) {
                        ex.printStackTrace();
                    }
                    sp.setDividerLocation(150);
                }else if (inforChange.equals(lastPathComponent)){
                    sp.setRightComponent(new JLabel("这里进行信息更新..."));
                    sp.setDividerLocation(150);
                }else if (inforStat.equals(lastPathComponent)){
                    sp.setRightComponent(new JLabel("这里进行信息统计..."));
                    sp.setDividerLocation(150);
                }else if (inforPrint.equals(lastPathComponent)){
                    sp.setRightComponent(new JLabel("这里进行信息打印..."));
                    sp.setDividerLocation(150);
                }
            }
        });
        sp.setRightComponent(new GradeInsComponent(jf));
        sp.setLeftComponent(tree);
        jf.add(sp);
        jf.setVisible(true);



    }

    public static void main(String[] args) throws Exception {
        new ManagerInterface().init();
    }
    private class MyRenderer extends DefaultTreeCellRenderer {
        private ImageIcon rootIcon = null;
        private ImageIcon userManageIcon = null;
        private ImageIcon bookManageIcon = null;
        private ImageIcon borrowManageIcon = null;
        private ImageIcon statisticsManageIcon = null;

        public MyRenderer() {
//            rootIcon = new ImageIcon(PathUtils.getRealPath("systemManage.png"));
//            userManageIcon = new ImageIcon(PathUtils.getRealPath("userManage.png"));
//            bookManageIcon = new ImageIcon(PathUtils.getRealPath("bookManage.png"));
//            borrowManageIcon = new ImageIcon(PathUtils.getRealPath("borrowManage.png"));
//            statisticsManageIcon = new ImageIcon(PathUtils.getRealPath("statisticsManage.png"));
        }

        //当绘制树的每个结点时，都会调用这个方法
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            //使用默认绘制
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

            ImageIcon image = null;
            switch (row) {
                case 0:
                    image = rootIcon;
                    break;
                case 1:
                    image = userManageIcon;
                    break;
                case 2:
                    image = bookManageIcon;
                    break;
                case 3:
                    image = borrowManageIcon;
                    break;
                case 4:
                    image = statisticsManageIcon;
                    break;
            }
            this.setIcon(image);
            return this;
        }
    }


}
